package com.mdxl.controller;

import com.mdxl.Entity.Customer;
import com.mdxl.mongo.CustomerRepository;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.elasticsearch.cluster.ClusterState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by chengjian on 2017/9/18.
 */
@RestController
@RequestMapping(value = "/mongo")
public class MongodbController {
    @Autowired
    private CustomerRepository repository;
    @Autowired
    private MongoTemplate mongoTemplate;
    //获取连接
   /* MongoClient mongoClient = new MongoClient("localhost", 27017);
    //获取数据库
    MongoDatabase database = mongoClient.getDatabase("chengjian");
    // 进入某个文档集
    MongoCollection<Document> collection = database.getCollection("customer");*/

    @RequestMapping("/create")
    public void createCollect(){
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        // 连接到数据库
        MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol");
        System.out.println("Connect to database successfully");
        mongoDatabase.createCollection("mdxl");
        System.out.println("集合创建成功");
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void addMongo() {
        Customer customer =new Customer();
        customer.setFirstName("heihei");
        customer.setLastName("zhenfan");
        //repository.save(new Customer("Alice", "Smith"));
        //repository.save(new Customer("Bob", "Smith"));
        mongoTemplate.save(customer);
        for (Customer customer1 : repository.findAll()) {
            System.out.println(customer1);
        }
    }
    @PostMapping("/addCus")
    public void addCus(){
        Customer customer =new Customer();
        customer.setFirstName("nizhenhao");
        customer.setLastName("zhenfanren");
        customer.setId(UUID.randomUUID().toString());
        addCustomer(customer);
    }
    @GetMapping("/getCus")
    public Customer getCus(){
         return getCustomer("nizhenhao");
    }

    //add
    public void addCustomer(Customer customer){
        mongoTemplate.save(customer);
    }
    //delete
    public void deleteCustomer(int id){
        Query query =new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query,Customer.class);
    }
    //update
    public void updateCustomer(Customer customer){
        Query query =new Query(Criteria.where("id").is(customer.getId()));
        Update update = new Update().set("lastName",customer.getLastName()).set("firstName",customer.getFirstName());
        //更新查询返回结果集的第一条
        mongoTemplate.updateFirst(query,update,Customer.class);
        //更新查询返回结果集的所有
        mongoTemplate.updateMulti(query,update,Customer.class);
    }
    //gain
    public Customer getCustomer(String firstName){
        Query query =new Query(Criteria.where("firstName").is(firstName));
        Customer customer = mongoTemplate.findOne(query,Customer.class);
        return customer;
    }


}

